<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>35.使用mixin实现多继承</title>
</head>

<body>
</body>

<script>
  //下面分拆功能使用 Mixin 实现多继承，使用代码结构更清晰。只让 Admin 继承 User 原型 

  function extend (sub, sup) {
    sub.prototype = Object.create(sup.prototype);
    sub.prototype.constructor = sub;
    Object.defineProperty(sub.prototype, 'constructor', {
      value: sub,
      enumerable: false
    });

  }
  const Credit = {
    total () {
      console.log("统计积分");
    }
  };
  const Request = {

    ajax () {
      console.log("请求后台");
    }
  }

  const Address = {
    getAddress () {
      console.log("获得收货地址");
    }
  }
  function User (name, age) {
    this.name = name;
    this.age = age;
  }


  User.prototype.show = function () {
    console.log(this.name, this.age);
  };
  function Admin (...args) {
    User.apply(this, args);
  }
  extend(Admin, User);
  //属性合并，把这些对象的属性合并到原型中即可
  Object.assign(Admin.prototype, Request, Credit, Address);

  let hd = new Admin("向军", 19);
  hd.show();
  hd.getAddress();
  //console.log(hd.constructor);
  hd.total(); //统计积分
  hd.ajax(); //请求后台
</script>

</html>